package gth.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

public class DatabaseUtils {
    private static ComboPooledDataSource cpds;

    private static DatabaseUtils databaseUtils;

    public DatabaseUtils() {
        cpds = new ComboPooledDataSource();
        try {
            cpds.setDriverClass("com.mysql.jdbc.Driver");
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        cpds.setJdbcUrl("jdbc:mysql://localhost:3306/gtnews?useSSL=true");
        cpds.setUser("root");
        cpds.setPassword("1234");

        cpds.setMinPoolSize(1);
        cpds.setAcquireIncrement(5);
        cpds.setMaxPoolSize(100);
        cpds.setMaxIdleTime(60);
        System.out.println("static{intit}");
    }

    public static Connection getConnection() throws SQLException {
        return getInstance().cpds.getConnection();
    }

    public static DatabaseUtils getInstance() {
        if (databaseUtils == null) {
            databaseUtils = new DatabaseUtils();
        }
        return databaseUtils;
    }
}

